package com.fdb.efp.nls.service.util;


import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class CalendarUtil {  
    /**
     * 格式化
     */
    private static SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
    /**
     * 判断是否年初第一天
     * @param date formate yyyy-MM-dd
     * @return
     */
    public static Boolean isYearFirstDay(Date date){        
        return  sf.format(date).equals(CalendarUtil.getFirstDayOfYear(date));
    }
    /**
     * 判断是否季度第一天
     * @param date
     * @return
     */
    public static Boolean isQuarterFirstDay(Date date){
      return sf.format(date).equals(CalendarUtil.getFirstDayOfQuarter(date));
    }
    /**
     * 判断是否月初第一天
     * @param date
     * @return
     */
    public static Boolean isMonthFirstDay(Date date){
      return sf.format(date).equals(CalendarUtil.getFirstDayOfMonth(date));
    }
    /**
     * 判断该日期属于那个季度的第一个月 
     * @param date
     * @return
     */
    public static int whichQuarter(Date date){
        Calendar cal= Calendar.getInstance();
        cal.setTime(date);
        int month=cal.get(Calendar.MONTH);
        int quarter=0;
        switch(month){
            case 0:
            case 1:
            case 2:quarter=0;break;
            case 3:
            case 4:
            case 5:quarter=3;break;
            case 6:
            case 7:
            case 8:quarter=6;break;
            case 9:
            case 10:
            case 11:quarter=9;break;
        }
        return quarter;
    }
    /**
     *  得到本季度 第一天 日期
     * @param date
     * @return
     */
   public static String getFirstDayOfQuarter(Date date){
       int quarter=CalendarUtil.whichQuarter(date);
       Calendar cal= Calendar.getInstance();
       cal.setTime(date);
       cal.set(Calendar.MONTH, quarter);
       cal.set(Calendar.DAY_OF_MONTH,1);
       date = cal.getTime();
      // date.setDate(1);
      return sf.format(date);
   }
/*   *//**
    *  得到本季度最后一天日期
    * @param date
    * @return
    *//*
   public static String getLastDayOfQuarter(Date date){
       int quarter=CalendarUtil.whichQuarter(date);
       
       Calendar cal= Calendar.getInstance();
       cal.setTime(date);
       cal.set(Calendar.MONTH, quarter+2);
       int maximum = cal.getMaximum(Calendar.DAY_OF_MONTH);
       
       cal.set(Calendar.DAY_OF_MONTH,maximum);
       
       date=cal.getTime();
      return sf.format(date);
   }*/
   /**
    * 得到该年的第一天日期
    * @param date
    * @return
    */
   public static String getFirstDayOfYear(Date date){
       SimpleDateFormat sf=new SimpleDateFormat("yyyy-01-01");
       return sf.format(date);
   }
   /**
    * 得到该年最后一天的日期
    * @param date
    * @return
    */
   public static String getLastDayOfYear(Date date){
       SimpleDateFormat sf=new SimpleDateFormat("yyyy-12-31");
       return sf.format(date);
   }
   /**
    * 获取月第一天日期
    * @param date
    * @return
    */
   public static String getFirstDayOfMonth(Date date) {
    SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-01");
    return sf.format(date);
   }
   /**
    * 获取本月最后一天
    * @param date
    * @return
    * @throws Exception
    */
   public static String getMonthLLastDay(String date) throws Exception {
       Date d = sf.parse(date);
       Calendar cal = Calendar.getInstance();
       cal.setTime(d);
       cal.set(Calendar.MONTH, cal.get(Calendar.MONTH)+1);
       cal.set(Calendar.DAY_OF_MONTH, 0);
       return sf.format(cal.getTime());
   }
   /**
    * 获取昨天
    * @param date
    * @return
    */
    public static String getYesterday(Date date) {
        Calendar cal= Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DAY_OF_MONTH,cal.get(Calendar.DAY_OF_MONTH)-1);
        date = cal.getTime();
        return sf.format(date);
    }
    /**
     * 获取明天
     * @param date
     * @return
     */
     public static String getTomorrow(Date date) {
         Calendar cal= Calendar.getInstance();
         cal.setTime(date);
         cal.set(Calendar.DAY_OF_MONTH,cal.get(Calendar.DAY_OF_MONTH)+1);
         date = cal.getTime();
         return sf.format(date);
     }
     
     /**
      * 日期 加day天
      * @param date
      * @param year
      * @return
      * @throws Exception
      */
     public static String addDay(String date,int day) throws Exception {
    	 SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
         Date d = sf.parse(date);
         Calendar cal = Calendar.getInstance();
         cal.setTime(d);
         cal.add(Calendar.DAY_OF_MONTH, day);
         return sf.format(cal.getTime());
     }
    public static void main(String[] args) {
        try {
        
        } catch(Exception e) {
            e.printStackTrace();
        } 
    }
}
